package com.hpe.sy.app;

import android.app.Activity;
import android.app.Application;
import android.graphics.Matrix;
import android.graphics.Path;
import android.graphics.PathMeasure;

import java.util.HashSet;
import java.util.Set;

/**************************
 * @Authoir phuang
 * @DateTime 16-11-2 下午4:30
 * @ClassName App
 **************************/
public class App extends Application {

    public static App instance;
    private Set<Activity> allActivities;

    public static App getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
    }

    /**
     * 添加activity到队列当中
     *
     * @param reActivity
     */
    public void registerActivity(Activity reActivity) {
        if (allActivities == null) {
            allActivities = new HashSet<>();
        }
        allActivities.add(reActivity);
    }

    /**
     * 移除activity从队列当中
     *
     * @param unActivity
     */
    public void unregisterActivity(Activity unActivity) {
        if (allActivities != null) {
            allActivities.remove(unActivity);
        }
    }

    /**
     * 退出APP并关闭所有activities
     */
    public void exitApp() {
        if (allActivities != null) {
            synchronized (allActivities) {
                for (Activity activity : allActivities) {
                    if (activity != null && !activity.isFinishing())
                        activity.finish();
                }
            }
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }
}
